package design.graduation.novelsystem.util;

import design.graduation.novelsystem.constant.Constant;
import design.graduation.novelsystem.model.domian.BookContent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: 洪少尉
 * @Date: 2020/4/17 23:40
 * @Description: 小说解析工具
 */
public class AnalyzeUtils {
	static Pattern pattern = Pattern.compile("(^\\s*第)(.{1,9})[章节集部篇回](\\s{1})(.*)($\\s*)");

	/**
	 * 解析txt
	 * @param fileAddr 解析文件路径
	 * @param bookId	收录的小说id
	 * @param bookName  收录的小说名称
	 * @return
	 */
	public static List<BookContent> analyzeTxt(String fileAddr, int bookId, String bookName){
		//小说文本
		String newStr=null;
		//章节id
		int chapterId = 0;
		//截取文本开始
		int begin = 0;
		//截取文本结束
		int end = 0;
		//小说类
		BookContent bookContent = new BookContent();
		//小说目录集合
		List<BookContent> bookContentList = new ArrayList<>();
		//章节目录
		String chapterName = null;
		try {
			//设置TXT编码格式
			String encoding = "GBK";
			// 文件路径
			File file = new File(fileAddr);
			//判断文件是否存在
			if(file.isFile() && file.exists()){
				bookContent.setBookName(bookName);
				bookContent.setBookId(bookId);
				//输入流
				InputStreamReader reader = new InputStreamReader(new FileInputStream(file),encoding);
				BufferedReader bufferedReader = new BufferedReader(reader);
				//当前行数的TXT文本内容
				String lineTxt = null;
				//章节内容
				String chapterContent = null;
				//循环退出标识
				boolean flag=false;
				//当前章节目录
				String newChapterName = null;
				int n = 0;
				//当前行数内容不为空时
				while ((lineTxt = bufferedReader.readLine()) != null){
					Matcher matcher = pattern.matcher(lineTxt);
					Matcher matcher1 = pattern.matcher(lineTxt);
					newStr=newStr + lineTxt;
					//正则表达式匹配成功
					while ( matcher.find()){
						//匹配所有,获取章节目录，且去除左右空格
						chapterName = matcher.group().trim();
						bookContent.setChapterName(newChapterName);
						begin = end;
						newChapterName = chapterName;
						end = newStr.indexOf(chapterName);
						if ( flag ){
							//设置循环标识
							flag = false;
							//退出循环
							break;
						}
						if(n==0) {
							begin = newStr.indexOf(chapterName);
						}
						n=1;
						flag=true;
					}
					while ( matcher1.find()){
						if ( begin != end){
							//获取章节内容
							chapterContent = newStr.substring(begin,end);
							//将章节内容保存到实体类中
							bookContent.setChapterContent(chapterContent);
							bookContent.setSaveTime(new Date());
							bookContent.setAvailable(Constant.AVAILABLE_TRUE);
							//System.out.println(bookContent);
							bookContent.setChapterId(++chapterId);
							BookContent newBookContent = new BookContent();
							newBookContent.setBookId(bookContent.getBookId());
							newBookContent.setBookName(bookContent.getBookName());
							newBookContent.setChapterName(bookContent.getChapterName());
							newBookContent.setChapterContent(bookContent.getChapterContent());
							newBookContent.setSaveTime(bookContent.getSaveTime());
							newBookContent.setChapterId(bookContent.getChapterId());
							newBookContent.setAvailable(bookContent.getAvailable());
							bookContentList.add(newBookContent);
						}
					}
				}
				bufferedReader.close();
			}else {
				System.out.println("找不到指定的文件");
			}
			bookContent.setAvailable(Constant.AVAILABLE_TRUE);
			bookContent.setChapterName(chapterName);
			bookContent.setChapterContent(newStr.substring(end));
			bookContent.setSaveTime(new Date());
			bookContent.setChapterId(++chapterId);
			bookContentList.add(bookContent);
			//System.out.println(bookContent);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bookContentList;
	}
}
